﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace PagSeguro.Mvc.HtmlHelpers
{
    public static class PagSeguroFormHtmlHelper
    {
        public static MvcForm PagSeguroForm(this HtmlHelper htmlHelper,
                                            string formAction = "https://pagseguro.uol.com.br/checkout/checkout.jhtml")
        {
            return PagSeguroForm(htmlHelper, new Dictionary<string, object>(), formAction);
        }

        public static MvcForm PagSeguroForm(this HtmlHelper htmlHelper,
                                            IDictionary<string, object> htmlAttributes,
                                            string formAction = "https://pagseguro.uol.com.br/checkout/checkout.jhtml")
        {
            TagBuilder builder = new TagBuilder("form");
            builder.MergeAttributes<string, object>(htmlAttributes);
            builder.MergeAttribute("target", "pagseguro");
            builder.MergeAttribute("action", formAction);
            builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(FormMethod.Post), true);
            htmlHelper.ViewContext.HttpContext.Response.Write(builder.ToString(TagRenderMode.StartTag));

            return new MvcForm(htmlHelper.ViewContext);
        }
    }
}
